<?php namespace Zm345\Com\SDK\OpenLib\ModuleParam;use Zmsun\Copen\OpenLib;abstract class AModuleParam{protected$xmlDoc;protected$openLib;protected$iden='';protected$extHtmAttrs;protected$supportedHtmExtAttrs=[];protected$showName;public function __construct(OpenLib$openLib){$this->openLib=$openLib;}public function load($xmlSource){if($xmlSource instanceof \SimpleXMLElement){$this->xmlDoc=$xmlSource;}elseif(!$this->xmlDoc=@simplexml_load_string($xmlSource)){return false;}return true;}public function getAttribute($name){if(isset($this->xmlDoc->attributes()->{$name})){return (string)$this->xmlDoc->attributes()->{$name};}}public function setAttribute($name,$value,$isCreate=true){if(isset($this->xmlDoc->attributes()->{$name})){$this->xmlDoc->attributes()->{$name}=$value;}elseif($isCreate){$this->xmlDoc->addAttribute($name,$value);}}protected function getTipsHtml($tips){$sumTips=iconv_substr($tips,0,16,'UTF-8');$allDes='';if(iconv_strlen($tips,'UTF-8')>16){$allDes=<<<S
<span class="intro J_popTipTrigger">[?]</span>
<div class="allTips">
	<p class="allPoptips J_allTips">{$tips}</p>
</div>
S;
}return sprintf('<div class="tips">%s%s</div>',$sumTips,$allDes);}public function __toString(){if(false===$str=$this->xmlElement->asXML()){throw new \RuntimeException(sprintf("The XML file format error(%s).",get_class($this)));}return preg_replace('/^<\?xml\s+version="1.0"\?>\s+/','',$str);}public function html(){$label=$this->getAttribute('label');$name=$this->getAttribute('name');$tips=$this->getTipsHtml($this->getAttribute('description'));$value=$this->value(true);$selectNum=$this->selectedCount();$jsonVal=htmlspecialchars(json_encode($value,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));$extAttrStr='';foreach($this->getExtHtmAttrs()as$extName=>$extVal){$extAttrStr.=' data-ext-'.strtolower($extName).'="'.htmlspecialchars($extVal).'"';}return <<<S
<div class="control-group">
	<label class="control-label">{$label}</label>
	<div class="control">
		<span class="widget-btn cParamTrigBtn" data-cparam-iden="{$this->iden()}">$this->showName</span>
		<input type="hidden" class="cParamData" name="{$name}" value="{$jsonVal}"{$extAttrStr}>
		<span class="param-item-sum cParamSum"> （已选择 <em>{$selectNum}</em> 项） </span>
		{$tips}
	</div>
</div>
S;
}public function iden($iden=null){if(!is_null($iden)){$this->iden=strval($iden);}return$this->iden;}protected function getExtHtmAttrs(){if(!isset($this->extHtmAttrs)){$this->extHtmAttrs=[];foreach($this->supportedHtmExtAttrs as$attr){if(!is_null($val=$this->getAttribute($attr))){$this->extHtmAttrs=[$attr=>$val];}}}return$this->extHtmAttrs;}abstract public function value($orig=false,&$errorMsg="");abstract public function selectedCount();}?>